package com.taikang.hmp.hbusiness.doctor.selfdiagnosis.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.frame.customer.bean.CustomerInfo;
import com.taikang.hmp.hbusiness.doctor.register.bean.Activation;
import com.taikang.hmp.hbusiness.doctor.selfdiagnosis.bean.AutognosisQuestion;
import com.taikang.hmp.hbusiness.doctor.selfdiagnosis.bean.CustomerResult;
import com.taikang.hmp.hbusiness.doctor.selfdiagnosis.bean.Position;
import com.taikang.hmp.hbusiness.doctor.selfdiagnosis.bean.Symptom;
import com.taikang.hmp.hbusiness.doctor.selfdiagnosis.service.SelfDiagnosisService;

public class SelfDiagnosisServiceImpl extends BaseServiceImpl implements
		SelfDiagnosisService {
	
	@Override
	public List<Dto> getSymptomList(Dto dto) {
		List<Dto> results = new ArrayList<Dto>();
		Position po = new Position();
		po.setBodyRegion(dto.getAsString("bodyRegion"));
		po.setSex(dto.getAsString("sex"));
		po.setAround(dto.getAsString("around"));
		
		List<Symptom> list = super.getNewDao().queryForList("autognosis_space.getSymptomList", po);
		if (list != null && !list.isEmpty()) {
			for(Symptom sympton : list){
				Dto  result  = new BaseDto();
				result.put("symptom", sympton.getSymptom());
				result.put("symptomid", sympton.getSymptom_id());
				results.add(result);
			}
		}
		return results;
	}
	@Override
	public List<Dto> getSlefDiagnosis(Dto paramDto) {
		AutognosisQuestion aq = new AutognosisQuestion();
		List<AutognosisQuestion> list = new ArrayList<AutognosisQuestion>();
		List<Dto> results = new ArrayList<Dto>();
		//点击症状时 获得第一个根问题 传入症状symptom="眩晕" 和 代表根问题表示 root="1" 其他问题选择是或否 root="0"
		//System.out.println(paramDto.getAsString("root"));
		if("1".equals(paramDto.getAsString("root"))){
			aq.setSymptom(paramDto.getAsString("symptom"));
			aq.setRoot(paramDto.getAsString("root"));
			list = super.getNewDao().queryForList("autognosis_space.getRootQuestion", paramDto.getAsString("symptom"));
			
		}else if("320".equals(paramDto.getAsString("root"))){
			aq.setSymptom(paramDto.getAsString("symptom"));
			aq.setRoot(paramDto.getAsString("root"));
			list = super.getNewDao().queryForList("autognosis_space.getRootQuestionFO", paramDto.getAsString("symptom"));
		}else if("321".equals(paramDto.getAsString("root"))){
			aq.setSymptom(paramDto.getAsString("symptom"));
			aq.setRoot(paramDto.getAsString("root"));
			list = super.getNewDao().queryForList("autognosis_space.getRootQuestionFOU", paramDto.getAsString("symptom"));
		}else{
			//maleis  maleno femaleis femaleno 转换成id传入 直到返回的四个为空值则结束，不再请求
			//aq.setId(paramDto.getAsString("id"));
			list = super.getNewDao().queryForList("autognosis_space.getQuestion", paramDto.getAsString("id"));
			
		}
		String id = UUID.randomUUID().toString();
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("hd_user_space.getCustomer_uid", paramDto.getAsString("uid"));
		String customerid = customerIdList.get(0).getCustomer_id();
		CustomerResult cr = new CustomerResult();
		cr.setRecord_id(id);
		cr.setCustomer_id(customerid);
		cr.setQuestion_no(list.get(0).getId());
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			cr.setOperate_date(sdf.parse(sdf.format(new Date())));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//保存用户自诊结果
		super.getNewDao().insert("autognosis_space.saveCustomerResult", cr);
		Dto  result  = new BaseDto();
		String ss = new String();
		result.put("id", list.get(0).getId());
		result.put("datacontent", list.get(0).getDatacontent());
		result.put("maleis", list.get(0).getMaleis());
		result.put("maleno", list.get(0).getMaleno());
		result.put("femaleis", list.get(0).getFemaleis());
		result.put("femaleno", list.get(0).getFemaleno());
		result.put("deapartment", list.get(0).getDepartment());
		results.add(result);
		return results;
	}
	@Override
	public void addQuestion(Activation he) {
		super.getNewDao().insert("autognosis_space.activation",he);
	}

}
